package cn.fintecher.gis.service;

import cn.fintecher.gis.entity.*;
import cn.fintecher.gis.enums.CommonShow;
import cn.fintecher.gis.enums.FlowType;
import cn.fintecher.gis.enums.MessageType;
import cn.fintecher.gis.model.request.FlowApplyRequest;
import cn.fintecher.gis.model.request.LayerInfoCreateRequest;
import cn.fintecher.gis.model.request.ReportInfoRequest;
import cn.fintecher.gis.model.session.SessionModel;
import cn.fintecher.gis.utils.BeanUtils;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 17:12 2019/4/29
 */

@Service("reportInfoService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ReportInfoService extends BaseService {



    /**
     * 新增报地信息
     */
    public void addReportInfo(ReportInfoRequest request, SessionModel model) throws Exception {
        ReportInfo reportInfo = new ReportInfo();
        LocalDate dateTime = LocalDate.parse(request.getApplyTime(), DateTimeFormat.forPattern("yyyy-MM-dd"));
        BeanUtils.copyPropertiesIgnoreNull(request, reportInfo);
        reportInfo.setApplyTime(dateTime.toDate());
        if (Objects.nonNull(request.getLayerBaseRequest())) {
            LayerInfoCreateRequest layerInfoCreateRequest = new LayerInfoCreateRequest();
            BeanUtils.copyPropertiesIgnoreNull(request.getLayerBaseRequest(), layerInfoCreateRequest);
            List<String> styleNames = new ArrayList<>();
            styleNames.add(request.getLayerBaseRequest().getStyleName());
            layerInfoCreateRequest.setStyleNames(styleNames);
            BusinessLayer businessLayer = getLayerInfo(layerInfoCreateRequest, FlowType.REPORT, model);
            reportInfo.setBusinessLayer(businessLayer);
        }
        Set<DistrictInfo> infoSet = getDistrictInfoByIds(request.getRegions());
        reportInfo.setRegions(infoSet);
        reportInfo = reportInfoRepository.save(reportInfo);
        if (Objects.nonNull(request.getFileIds()) && !request.getFileIds().isEmpty()) {
            List<ReportFile> list = new ArrayList<>();
            for (String fileId : request.getFileIds()) {
                ReportFile reportFile = new ReportFile();
                reportFile.setReportId(reportInfo.getId());
                reportFile.setFileId(fileId);
                list.add(reportFile);
            }
            reportFileRepository.saveAll(list);
        }
        //添加流程申请
        FlowApplyRequest flowApplyRequest = new FlowApplyRequest();
        flowApplyRequest.setApplyId(reportInfo.getId());
        flowApplyRequest.setApplyUserId(model.getId());
        flowApplyRequest.setType(FlowType.REPORT);
        if (request.getWhether().equals(CommonShow.YES)) {
            flowApplyRequest.setNextUserId(request.getNextUserId());
        }
        flowApplyRequest.setWhether(reportInfo.getWhether());
        flowService.flowApply(flowApplyRequest, MessageType.REPORT);
    }

    /**
     * 根据项目名称查询对应的项目信息
     *
     * @param name
     * @return
     */
    public List<ReportInfo> getReportInfoByName(String name) {
        List<ReportInfo> list = new ArrayList<>();
        Iterator<ReportInfo> infoIterator = reportInfoRepository.findAll(QReportInfo.reportInfo.name.like("%".concat(name).concat("%"))).iterator();
        while (infoIterator.hasNext()) {
            ReportInfo reportInfo = infoIterator.next();
            if (!flowService.exitRefuse(reportInfo.getId())) {
                list.add(reportInfo);
            }
        }
        return list;
    }

    /**
     * 根据id查询报地信息
     *
     * @param id
     * @return
     */
    public ReportInfo getReportInfoById(String id) {
        ReportInfo info = new ReportInfo();
        reportInfoRepository.findById(id).ifPresent(reportInfo -> {
            BeanUtils.copyPropertiesIgnoreNull(reportInfo, info);
            return;
        });
        return info;
    }
}
